using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library;
using ECommerce.Library.Web;

public partial class myaccount_change_password : SitePublicBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (SessionManager.IsUserLoggedIn)
            {
                if (SessionManager.ReturnUrl != null)
                {
                    string returnUrl = SessionManager.ReturnUrl;
                    SessionManager.ReturnUrl = null;
                    Response.Redirect(returnUrl);
                }
                else
                {
                    SetPageTitleAndBreadcramb();
                }

            }
            else
            {
                Response.Redirect(string.IsNullOrEmpty(SupplierId) ? CustomSiteMap.Login : "~/partner/" + SupplierId + "/login.aspx");
            }
        }

    }


    private void SetPageTitleAndBreadcramb()
    {
        ContentPlaceHolder ph = (ContentPlaceHolder)this.Master.Master.FindControl("cphMiddleContent");

        Literal litPageTitle = (Literal)ph.FindControl("litPageTitle");
        Literal litCurrentPage = (Literal)ph.FindControl("litCurrentPage");

        if (litPageTitle != null)
        {
            litCurrentPage.Text = litPageTitle.Text = "Change Password";

        }
    }


    protected void imbModify_Click(object sender, EventArgs e)
    {
        try
        {            
            if (Page.IsValid)
            {
                string emailAddress = tbxEmailAddress.Text;
                string password = tbxCurrentPassword.Text;
                string newPassword = tbxNewPassword.Text;
                if (!RegExUtil.IsEmail(emailAddress) || !RegExUtil.IsPassword(password) ||!RegExUtil.IsPassword(newPassword))
                {
                    uccResponseMessage.SetFailMessage("Provided email address and password do not match.");
                    uccResponseMessage.Visible = true;
                }
                if (AutoGearSiteUser.IsLoginValid(emailAddress, password))
                {
                    CustomerUser customerUser = CustomerUser.GetCustomerUserByUserName(emailAddress);
                    if (customerUser.UpdatePassword(newPassword))
                    {
                        uccResponseMessage.SetSuccessMessage("Your password has been successfully changed.");
                        uccResponseMessage.Visible = true;
                    }
                    else {
                        uccResponseMessage.SetFailMessage("Modifying your account information failed. Please try again");
                        uccResponseMessage.Visible = true;
                    }
                }
                else
                {
                    uccResponseMessage.SetFailMessage("Provided email address and password do not match.");
                    uccResponseMessage.Visible = true;
                }
            }
        }
        catch (Exception ex)
        {
            uccResponseMessage.SetFailMessage(ex.Message);
            uccResponseMessage.Visible = true;
        }
    }
}
